<template>
    <input id="file" type="file" name="avatar" ref="file" @change="updataImg">
    <div class="container">
        <div class="main">
            <form method="post" enctype="multipart/form-data">
                <el-icon class="avatar-uploader-icon" @click="uploadImg">
                    <Plus />
                </el-icon>
            </form>
            <div class="avatar-box">
                <img :src="imageUrl" width="90" height="90" class="avatar" />
                <span class="title">{{ tip }}</span>
            </div>
        </div>
        <el-button :type="btnType" class="update" @click="submitUpload">
            更新
        </el-button>
    </div>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import { Util } from '@/Util/'
import { useUserStore } from '@/store';
import axios from 'axios';
const User = useUserStore()
const { avatar, username } = User.$state.user
const file = ref()
const imageUrl = ref(Util.getImgUrl(avatar))
const tip = ref('当前头像')
let btnType = ref<any>('info')
const uploadImg = () => {
    file.value.click()
}

const updataImg = () => {
    let rawFile = file.value.files[0]
    let reader = new FileReader()
    reader.onload = function () {
        if (imageValidata(rawFile)) {
            btnType.value = 'primary'
            tip.value = '预览头像'
            imageUrl.value = this.result as string
        }
    }
    reader.readAsDataURL(rawFile)
}
const submitUpload = async () => {
    let rawFile = file.value.files[0]
    if (imageValidata(rawFile)) {
        let { data } = await axios.post('/user/avatar', {
            username,
            avatar: rawFile
        }, {
            headers: {
                "Content-Type": "multipart/form-data"
            }
        })
        if (data.code == '0000') {
            ElMessage.success(data.msg)
            User.setUser(data.data)
        }
    }
}

let imageValidata = (rawFile: any) => {
    if (!rawFile) {
        ElMessage.error('头像为空')
        return false
    } else if (rawFile.type.split('/')[0] !== 'image') {
        ElMessage.error('头像不为image文件')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('头像大小不能超过2MB')
        return false
    }
    return true
}
</script>


<style scoped lang="less">
.container {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 50px;

    .main {
        display: flex;

        .el-icon.avatar-uploader-icon {
            border: 1px dashed #ccc;
            font-size: 50px;
            color: #909399;
            background-color: #f1f2f5;
            width: 150px;
            height: 150px;
            border-radius: 5px;
            text-align: center;
            transition: 1s;
            margin-right: 35px;

            &:hover {
                background-color: #d2d6dc;
            }
        }

        .avatar-box {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding-left: 35px;
            border-left: 1px solid #ccc;

            .avatar {
                border-radius: 50%;
                border: 1px solid #eee;
            }

            .title {
                margin-top: 10px;
                color: #99a2aa;
                font-size: 12px;
            }
        }

    }
}

.update {
    margin-top: 25px;
    width: 140px;
    height: 45px;
}

#file {
    display: none;
}
</style>